package com.rskj.cashbook.controller;

import com.rskj.cashbook.iexception.BusinessException;
import com.rskj.cashbook.response.ResponseEntity;
import com.rskj.cashbook.service.IDictDataService;
import com.rskj.cashbook.vo.DictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典数据控制器
 * @author fudaopin
 */
@RestController
@RequestMapping("/api/dict-data")
@CrossOrigin(origins = "*")
public class DictDataController {

    @Autowired
    private IDictDataService dictDataService;

    /**
     * 根据类型ID获取字典数据
     */
    @GetMapping("/type/{typeId}")
    public ResponseEntity<List<DictData>> getByTypeId(@PathVariable String typeId) {
        try {
            List<DictData> list = dictDataService.getByTypeId(typeId);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            throw new BusinessException("获取字典数据列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据类型编码获取字典数据
     */
    @GetMapping("/type-code/{typeCode}")
    public ResponseEntity<List<DictData>> getByTypeCode(@PathVariable String typeCode) {
        try {
            List<DictData> list = dictDataService.getByTypeCode(typeCode);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            throw new BusinessException("获取字典数据列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据类型ID获取启用的字典数据
     */
    @GetMapping("/type/{typeId}/enabled")
    public ResponseEntity<List<DictData>> getEnabledByTypeId(@PathVariable String typeId) {
        try {
            List<DictData> list = dictDataService.getEnabledByTypeId(typeId);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            throw new BusinessException("获取启用的字典数据列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据类型编码获取启用的字典数据
     */
    @GetMapping("/type-code/{typeCode}/enabled")
    public ResponseEntity<List<DictData>> getEnabledByTypeCode(@PathVariable String typeCode) {
        try {
            List<DictData> list = dictDataService.getEnabledByTypeCode(typeCode);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            throw new BusinessException("获取启用的字典数据列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取字典数据
     */
    @GetMapping("/{id}")
    public ResponseEntity<DictData> getById(@PathVariable String id) {
        try {
            DictData dictData = dictDataService.getById(id);
            if (dictData != null) {
                return ResponseEntity.ok(dictData);
            } else {
                throw new BusinessException("没有查询到字典数据");
            }
        } catch (Exception e) {
            throw new BusinessException("获取字典数据失败：" + e.getMessage());
        }
    }

    /**
     * 根据编码获取字典数据
     */
    @GetMapping("/code/{code}")
    public ResponseEntity<DictData> getByCode(@PathVariable String code) {
        try {
            DictData dictData = dictDataService.getByCode(code);
            if (dictData != null) {
                return ResponseEntity.ok(dictData);
            } else {
                throw new BusinessException("没有查询到字典数据");
            }
        } catch (Exception e) {
            throw new BusinessException("获取字典数据失败：" + e.getMessage());
        }
    }

    /**
     * 根据值获取字典数据
     */
    @GetMapping("/value/{value}")
    public ResponseEntity<DictData> getByValue(@PathVariable String value) {
        try {
            DictData dictData = dictDataService.getByValue(value);
            if (dictData != null) {
                return ResponseEntity.ok(dictData);
            } else {
                throw new BusinessException("没有查询到字典数据");
            }
        } catch (Exception e) {
            throw new BusinessException("获取字典数据失败：" + e.getMessage());
        }
    }

    /**
     * 保存字典数据
     */
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody DictData dictData) {
        try {
            // 检查编码是否已存在
            if (dictDataService.isCodeExistsByTypeId(dictData.getTypeId(), dictData.getCode())) {
                throw new BusinessException("字典数据编码已存在");
            }
            
            int result = dictDataService.save(dictData);
            if (result > 0) {
                return ResponseEntity.ok("保存成功");
            } else {
                return ResponseEntity.ok("保存失败");
            }
        } catch (Exception e) {
            return ResponseEntity.ok("保存字典数据失败：" + e.getMessage());
        }
    }

    /**
     * 更新字典数据
     */
    @PutMapping("/update")
    public ResponseEntity<String> update(@RequestBody DictData dictData) {
        try {
            int result = dictDataService.update(dictData);
            if (result > 0) {
                return ResponseEntity.ok("更新成功");
            } else {
                return ResponseEntity.ok("更新失败");
            }
        } catch (Exception e) {
            return ResponseEntity.ok("更新字典数据失败：" + e.getMessage());
        }
    }

    /**
     * 删除字典数据
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> delete(@PathVariable String id) {
        try {
            int result = dictDataService.delete(id);
            if (result > 0) {
                return ResponseEntity.ok("删除成功");
            } else {
                return ResponseEntity.ok("删除失败");
            }
        } catch (Exception e) {
            return ResponseEntity.ok("删除字典数据失败：" + e.getMessage());
        }
    }

    /**
     * 根据类型ID删除字典数据
     */
    @DeleteMapping("/type/{typeId}")
    public ResponseEntity<String> deleteByTypeId(@PathVariable String typeId) {
        try {
            int result = dictDataService.deleteByTypeId(typeId);
            if (result > 0) {
                return ResponseEntity.ok("删除成功");
            } else {
                return ResponseEntity.ok("删除失败");
            }
        } catch (Exception e) {
            return ResponseEntity.ok("删除字典数据失败：" + e.getMessage());
        }
    }
}
